//------------------------------------------------------------------------------
#include <QtGui>
#include <QSqlRecord>
#include "CBLsNonFactureFrm.h"
#include "CMyTable.h"
#include "CCalendrierFrm.h"
#include "CMySQL.h"
//------------------------------------------------------------------------------
CBLsNonFactureFrm::CBLsNonFactureFrm(QWidget *parent) : QDialog(parent)
{
	setupUi(this);
	
	dateEdit->setDate(CMySQL::getInstance()->getCurrentDate());
	
	createGrille();
	
	loadGrille();
}
//------------------------------------------------------------------------------
void CBLsNonFactureFrm::createGrille(void)
{
	QList<CColumnModel *> *columnModel=new QList<CColumnModel *>();
	
	columnModel->append(new CColumnModelString(cnbnfCode, "nomBL", tr("BL"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnbnfDocument, "document", tr("Document"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDate(cnbnfDate, "creation", tr("Du"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnbnfCodeClient, "codeClient", tr("Client"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnbnfNomClient, "nomLivraison", tr("Nom"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelDouble(cnbnfMontant, "montant", tr("Montant"), Qt::AlignRight, NULL, Qt::ItemIsEnabled));
	columnModel->append(new CColumnModelString(cnbnfPretAFacture, "pretAFacture", tr("Terminer ?"), Qt::AlignLeft, NULL, Qt::ItemIsEnabled));
	
	tableBLsNonFacture->setColumnsModel(columnModel);
}
//------------------------------------------------------------------------------
void CBLsNonFactureFrm::loadGrille(void)
{
	QString requete;
	QSqlQuery *query;
	
	requete="SELECT tb.nomBL, tb.document, tb.creation, tb.codeClient, tb.nomLivraison, ";
	requete+="SUM(lb.quantite*lb.prixUnitaireHT) AS montant, tb.pretAFacture, tb.code ";
	requete+="FROM teteBLs AS tb ";
	requete+="INNER JOIN ligneBLs AS lb ON lb.bl=tb.code ";
	requete+="LEFT JOIN ligneFactures AS lf ON lf.idLigneBL=lb.id ";
	requete+="WHERE lf.idLigneBL IS NULL ";
	requete+="GROUP BY tb.code ";
	requete+="ORDER BY tb.codeClient, tb.code";
	
	if(CMySQL::getInstance()->execQuery(requete, &query))
	{
		tableBLsNonFacture->loadGrille(query, "code");
	}
	delete query;
}
//------------------------------------------------------------------------------
void CBLsNonFactureFrm::reject(void)
{
	listBLsSelected.clear();
	
	QDialog::reject();
}
//------------------------------------------------------------------------------
void CBLsNonFactureFrm::on_tableBLsNonFacture_cellDoubleClicked(int row, int column)
{
	int X;
	int idx;
	int code;
	QBrush brush;
	
	code=tableBLsNonFacture->item(row, cnbnfCode)->data(Qt::UserRole).toInt();
	if((idx=listBLsSelected.indexOf(code)) != -1)
	{
		brush=QBrush(Qt::white);
		listBLsSelected.removeAt(idx);
	}else
	{
		brush=QBrush(QColor(170, 255, 170));
		listBLsSelected.append(code);
	}
	
	for(X=0;X<tableBLsNonFacture->getColumnModelCount();X++)
	{
		tableBLsNonFacture->item(row, X)->setBackground(brush);
	}
}
//------------------------------------------------------------------------------
void CBLsNonFactureFrm::on_tableBLsNonFacture_beforeInsert(const QSqlQuery *query, CMyTableWidgetItem *item)
{
	QSqlRecord rec=query->record();
	
	if(rec.value("pretAFacture").toString() == "OUI") 
	{
		int code=rec.value("code").toInt();
		item->setBackground(QColor(170, 255, 170));
		
		if(listBLsSelected.indexOf(code) == -1) 
		{
			listBLsSelected.append(code);
		}
	}
}
//------------------------------------------------------------------------------
void CBLsNonFactureFrm::on_cmdFactureBLsSelected_clicked(bool checked)
{
	CCalendrierFrm *calendrierFrm=new CCalendrierFrm();
	QDate date=QDate::currentDate();
	
	if(date.day() != date.daysInMonth())
	{
		date.setDate(date.year(), date.month(), 1);
		date=date.addDays(-1);
	}
	
	calendrierFrm->setDate(date);
	
	if(calendrierFrm->exec() == QDialog::Accepted)
	{
		dateEdit->setDate(calendrierFrm->getDate());
		QDialog::accept();
	}
	
	delete calendrierFrm;
}
//------------------------------------------------------------------------------
void CBLsNonFactureFrm::on_cmdSelectBLMois_clicked(bool) {
	int Y;
	int monthRef=dateEdit->date().month();
	int yearRef=dateEdit->date().year();
	
	listBLsSelected.clear();
	
	for(Y=0;Y<tableBLsNonFacture->rowCount();Y++) {
		QDate date=QDate::fromString(tableBLsNonFacture->item(Y, cnbnfDate)->text(), "dd/MM/yyyy");
		int month=date.month();
		int year=date.year();
		int X;
	
		for(X=0;X<tableBLsNonFacture->getColumnModelCount();X++) {
			tableBLsNonFacture->item(Y, X)->setBackground(QBrush(Qt::white));
		}
		
		if(year == yearRef && month == monthRef) {
			on_tableBLsNonFacture_cellDoubleClicked(Y, 0);
		}
	}
}
//------------------------------------------------------------------------------